// pages/prepay/index.js
import {request,fail,ok} from "../../util"
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressShow:false,
    payPrice: undefined,
    totalAmount: undefined,
    totalPostage: undefined,
    totalPrice: undefined,
    items:[],
    addressList:[],
    address:undefined,
    addressChoosed:undefined,
    cartIds:[]
  },
  onAddressShow(){
    this.setData({addressShow:!this.data.addressShow})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.loadDefaultAddress()
    if(!options.cartIds){
      fail("未选择商品")
      wx.navigateBack()
    }
    let cartIds=  options.cartIds.split(",").map(e=>parseInt(e))
    this.setData({cartIds})
    request({
      url:"/order/prepare",
      method:"POST",
      data:cartIds
    }).then(res=>{
        //console.log(res);
        this.setData({
          totalAmount:res.data.totalAmount,
          totalPostage:res.data.totalPostage,
          payPrice:res.data.payPrice,
          totalPrice:res.data.totalPrice,
          items:res.data.items
        })
    })
  },

  loadAddressList(){
    request({
      url:"/address/list?pageShow=false"
    }).then(res=>{
        this.setData({addressList:res.data.list})
    })
  },
  loadDefaultAddress(){
    request({
      url:"/address/default"
    }).then(res=>{
      this.setData({address:res.data})
    })
  },
  toAddress(){
    wx.navigateTo({
      url: '/pages/address/index',
    })
  },
  toOrderPay(){
    request({
      url:"/order/create",
      method:"POST",
      data:{
        addressId:this.data.address.addressId,
        cartIds:this.data.cartIds
      }
    }).then(res=>{
      //console.log(res.data);
      request({
        url:"/pay/request",
        method:"POST",
        data:{
          orderNo: res.data,
          channel: 0
        }
      }).then(res2=>{
        wx.requestPayment(
          {
            timeStamp: res2.data.timeStamp,
            nonceStr: res2.data.nonceStr,
            package: res2.data.package,
            signType: res2.data.signType,
            paySign: res2.data.paySign,
            success (res3) {
              //console.log(res);
              request({
                url:"/pay/query?orderNo="+res.data
              }).then((res5)=>{
                  ok("订单支付成功")
                  setTimeout(()=>{
                    wx.navigateBack()
                  },2000)
              })
             },
            fail (res4) {
              //console.log(res);
              fail("订单未支付")
              setTimeout(()=>{
                wx.navigateBack()
              },2000)
             }
          }
        )
      })
    })
  },
  onDeleteAddress(e){
    let addresId= e.currentTarget.dataset.id
    Dialog.confirm({
      title: '删除地址',
      message: '确定删除地址？',
    })
      .then(() => {
        // on confirm
        request({
          url:"/address/delete?ids="+addresId,
          method:"POST"
        }).then(res=>{
          this.loadAddressList()
        })
      })
      .catch(() => {
        // on cancel
      });
  },
  chooseAddress(e){
    let addressChoosed= e.detail
    this.setData({addressChoosed})
    this.data.addressList.forEach(e=>{
      if(e.addressId===parseInt(addressChoosed)){
        this.setData({address:e})
      }
    })
    this.setData({addressShow:!this.data.addressShow})
  },
  toEditeAddress(e){
    let addresId= e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/address/index?id='+addresId,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.loadAddressList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})